package com.gxpt.app;

import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.os.Build;
import android.os.StrictMode;
import android.os.Vibrator;

import com.baidu.mapapi.SDKInitializer;
import com.gxpt.app.codes.service.LocationService;
import com.utils.logger.Logger;

import org.xutils.x;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by dufeng on 2017/8/4.
 */

public class MyApp extends Application {

    private static String TAG = "gxpt";

    private List<Activity> activityList = new LinkedList<Activity>();

    private static MyApp mInstance;

    public LocationService locationService;
    public Vibrator mVibrator;

    // 单例模式中获取唯一的ExitApplication 实例
    public static MyApp getInstance(){
        if(mInstance == null){
            mInstance = new MyApp();
        }
        return mInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(false); //输出debug日志，开启会影响性能

        /***
         * 初始化定位sdk，建议在Application中创建
         */
        locationService = new LocationService(getApplicationContext());
        mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
        SDKInitializer.initialize(getApplicationContext());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }

        Logger.init(TAG);

    }

    /***
     * 添加Activity 到容器中
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        // LogTools.printLog("添加---" + activity.getClass().toString());
        activityList.add(activity);
    }

    /***
     * 删除容器中Activity
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        // LogTools.printLog("删除了---" + activity.getClass().toString());
        activityList.remove(activity);
    }

    /***
     * 遍历所有Activity 并finish
     */
    public void exit() {

        for (Activity activity : activityList) {
            activity.finish();
        }
        System.exit(0);

    }

    /***
     * 关闭指定的activity
     *
     * @param
     */
    public void closeOneActivity(String msg) {
        for (int i = 0; i < activityList.size(); i++) {
            Logger.i("==="+activityList.get(i).getClass().getSimpleName());
            if (activityList.get(i).getClass().getSimpleName().equals(msg)) {
                activityList.get(i).finish();
                activityList.remove(i);
            }
        }
    }

    /***
     * 关闭其他activity
     *
     * @param ibeab
     */
    public void exitAnthor(Activity ibeab) {

        for (Activity activity : activityList) {
            if (ibeab != activity) {
                activity.finish();
            }
        }
    }


    public void exitApp(){
        for (Activity activity : activityList) {
            activity.finish();
        }
    }
}
